package com.school.service.impl;

import com.school.dao.UserRoleMapper;
import com.school.model.UserRole;
import com.school.model.UserRoleExample;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class UserRoleServiceImpl {
    private Logger logger = LoggerFactory.getLogger(this.getClass());
    UserServiceImpl userService;

    @Resource
    private UserRoleMapper userRoleMapper;

    public UserRole queryById(Integer id) {
        UserRoleExample userRoleExample = new UserRoleExample();
        UserRoleExample.Criteria criteria = userRoleExample.createCriteria();
        criteria.andIdEqualTo(id);
        criteria.andDeletedEqualTo((byte) 0);
        List<UserRole> userRoles = userRoleMapper.selectByExampleSelective(userRoleExample);
        return userRoles.size() == 0 ? null : userRoles.get(0);
    }

    public List<UserRole> queryByUserId(Integer userId) {
        UserRoleExample userRoleExample = new UserRoleExample();
        UserRoleExample.Criteria criteria = userRoleExample.createCriteria();
        criteria.andUserIdEqualTo(userId);
        criteria.andDeletedEqualTo((byte) 0);
        return userRoleMapper.selectByExampleSelective(userRoleExample);
    }


    public void add(Integer userId, int roleId) {
        UserRole userRole = new UserRole();
        userRole.setUpdateTime(LocalDateTime.now());
        userRole.setAddTime(LocalDateTime.now());
        userRole.setUserId(userId);
        userRole.setRoleId(roleId);
        userRoleMapper.insertSelective(userRole);
    }
}
